/*
** incantation.c for Incantation in /home/vriely/Dropbox/work/Unix/Zappy_rendu/serv-src
**
** Made by Samuel Vrielynck
** Login   <vriely_s@epitech.net>
**
** Started on  Wed Jul 04 19:35:24 2012 Samuel Vrielynck
** Last update Fri Jul 20 09:27:53 2012 guillaume renard
*/

#include		<stdlib.h>

#include		"server.h"

static const int	res_need[7][NB_OBJECT + 1] =
{
  { 0, 1, 0, 0, 0, 0, 0, 1 },
  { 0, 1, 1, 1, 0, 0, 0, 2 },
  { 0, 2, 0, 1, 0, 2, 0, 2 },
  { 0, 1, 1, 2, 0, 1, 0, 4 },
  { 0, 1, 2, 1, 3, 0, 0, 4 },
  { 0, 1, 2, 3, 0, 1, 0, 6 },
  { 0, 2, 2, 2, 2, 2, 1, 6 }
};

static char		do_nb_player(t_list *gamers_in, t_client *graphic,
    t_incantation *inca)
{
  if (gamers_in->len == res_need[inca->lvl][JOUEUR])
  {
    gamers_in->foreach(gamers_in, &foreach_inc_lvl, graphic);
    return (1);
  }
  else
    gamers_in->foreach(gamers_in, &foreach_inc_fail, graphic);
  return (0);
}

static char		check_map_ressource(int square[], unsigned char lvl)
{
  if (square[LINEMATE] == res_need[lvl][LINEMATE] &&
      square[DERAUMERE] == res_need[lvl][DERAUMERE] &&
      square[SIBUR] == res_need[lvl][SIBUR] &&
      square[MENDIANE] == res_need[lvl][MENDIANE] &&
      square[PHIRAS] == res_need[lvl][PHIRAS] &&
      square[THYSTAME] == res_need[lvl][THYSTAME])
    return (1);
  return (0);
}

char			*incantation(t_client *client, t_game *game)
{
  unsigned char		ok;

  if (game->graphic != NULL)
    serv_monitor("pic", client, game, game->graphic);
  client->gamer->action = 300 * MILLI / game->time;
  ok = check_map_ressource(
      game->obj[client->gamer->coord.y][client->gamer->coord.x].inv,
      client->gamer->level - 1);
  game->event->add_back(game->event,
      new_event(INCATATION, 300 * MILLI / game->time,
	new_incantation(client->gamer->coord, client->gamer->level - 1,
	  client, ok)));
  client->send->concat(client->send, "elevation en cours\n");
  return (NULL);
}

static char		check_nb_player(t_list *clients, t_game *game,
    t_incantation *inca)
{
  char			ret;
  t_list		*gamers_in;
  t_item		*cur;
  t_gamer		*gamer;

  cur = clients->head;
  gamers_in = new_list();
  if (game->obj[inca->coord.y][inca->coord.x].inv[JOUEUR] ==
      res_need[inca->lvl][JOUEUR])
  {
    while (cur)
    {
      if ((gamer = ((struct s_client *)cur->content)->gamer) != NULL)
	if (gamer->coord.x == inca->coord.x && gamer->coord.y == inca->coord.y
	    && gamer->level == inca->lvl + 1)
	  gamers_in->add_back(gamers_in, cur->content);
      cur = cur->next;
    }
    ret = do_nb_player(gamers_in, game->graphic, inca);
  }
  free(gamers_in);
  return (ret);
}

char			handle_inca(t_event *event, t_game *game,
    t_list *clients, long unsigned int elapsed_time)
{
  t_incantation		*inca;

  inca = (struct s_incantation *)event->data;
  if (inca->ok == 1)
  {
    if (inca->client != NULL && inca->client->gamer != NULL &&
	is_alive(game->time, *(inca->client->gamer), elapsed_time))
    {
      if ((inca->ok = check_map_ressource(
	      game->obj[inca->coord.y][inca->coord.x].inv, inca->lvl)) == 1)
	inca->ok = check_nb_player(clients, game, inca);
      else
	inca->client->send->concat(inca->client->send, "ko\n");
    }
  }
  else
    inca->client->send->concat(inca->client->send, "ko\n");
  send_data_incan(game->graphic, inca, inca->ok);
  if (game->graphic != NULL)
    game->event_mnt->add_back(game->event_mnt, \
	new_event(INCANT_MNT, 0, NULL));
  free(inca);
  return (1);
}
